﻿package stroe.lvyang.rsms;

import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMile {

    public  void sent(String msg) throws Exception {
        //创建propertie
        Properties properties = new Properties();
        // 连接协议
        properties.put("mail.transport.protocol", "smtp");
        // SMTP 服务器名，可以从 QQ 邮箱的帮助文档查到
        // 文档地址：https://service.mail.qq.com/cgi-bin/help?subtype=1&no=167&id=28
        properties.put("mail.smtp.host", "smtp.qq.com");
        // SMTP 服务器端口号，可以从 QQ 邮箱的帮助文档查到端口为 465 或 587
        properties.put("mail.smtp.port", 465);
        // 是否需要身份验证
        properties.put("mail.smtp.auth", "true");
        // 是否使用 ssl 安全连接
        properties.put("mail.smtp.ssl.enable", "true");
        // 是否输出控制台信息
        properties.put("mail.debug", "true");
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        //创建message对象使用session配置
        MimeMessage message = new MimeMessage(session);
        //发件人
        message.setFrom("952835607@qq.com");
        //收件人
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("952835607@qq.com"));
        //标题
	            /*	message.setSubject("你好，帅哥");
		       //文件内容      //	message.setText("你好本次你好帅：12312312");
		        message.setContent("<h1 style='color:red;'>你好帅</h1>", "text/html;charset=utf-8");*/
        message.setText(msg);
        //创建transprot对象
        Transport transport = null;
        transport = session.getTransport();
        //链接服务器
        transport.connect("pop.qq.com","952835607@qq.com","xxxxxxxxxx");
        //发送信息
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

 public static void main(String[] args) {
        SendMile sendMile = new SendMile();
        try {
            sendMile.sent("test");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
